﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using SimpleSpreadsheet.Core;
using SimpleSpreadsheet.Core.Expressions;

namespace SimpleSpreadsheet.Functions
{
    /// <summary>
    /// Evaluates the sum of decimal arguments
    /// </summary>
    [Export(typeof (IFunction))]
    public class Sum : IFunction
    {
        #region IFunction Members

        public string Name
        {
            get { return "SUM"; }
        }

        public decimal Evaluate(IList<BaseExpression> arguments, ExpressionVisitor expressionVisitor)
        {
            decimal sum = 0;
            foreach (BaseExpression expr in arguments)
            {
                sum += Convert.ToDecimal(expressionVisitor.Evaluate(expr));
            }
            return sum;
        }

        #endregion
    }
}